﻿<cfcomponent displayname="Company Setting Manage Company" hint="Company Setting Manage Company" extends="Controller" output="false">

	<cffunction name="init" access="public" hint="init" description="init">
		
		<cfscript>
			
			filters(through="isLoggedIn");
			
			flashInsert(menu="Company &gt; Setting &gt; Manage Company");
			
			provides("html,json");
			
			return this;
			
		</cfscript>
		
	</cffunction>
	
	<cffunction name="home" access="public" hint="home" description="home">
		
		<cfscript>
			
			company = model("company").findAll(select="id,companyname,address,phone,fax");
			
		</cfscript>
		
	</cffunction>
	
	<cffunction name="companyList" access="public" hint="company list" description="company list">
		<cfsetting showdebugoutput="false" enablecfoutputonly="true" >
		
		<cfscript>
			if (Len(Trim(decryptHex(form.qtype))) and Len(Trim(decryptHex(form.query)))) {
				companies = model("company").findAll(
					select="id,companyname,address,phone,fax", 
					where="#decryptHext(form.qtype)# = '#decryptHex(form.query)#'", 
					order="#decryptHex(form.sortname)# #form.sortorder#"
				);
			}
			else {
				companies = model("company").findAll(
					select="id,companyname,address,phone,fax", 
					order="#decryptHex(form.sortname)# #form.sortorder#"
				);
			}
			
			grid = flexigridstruct(query="#companies#", columns="companyname,address,phone,fax", page="#form.page#", rows="#form.rp#", key="id");
			
			renderWith(grid);
		</cfscript>		
		
	</cffunction> 
	
	<cffunction name="addCompany" access="public" hint="add company" description="add company">
				
	</cffunction> 
	
	<cffunction name="create" access="public" hint="create" description="create">
		
		<cfscript>
			
			if (not Len(Trim(params.company))) {
				flashInsert(error="Required : Company");
				redirectTo(action="home");
			}
			
			local.company = model("company").new();
			
			local.company.companyName = params.company;
			local.company.address = params.address;
			local.company.phone = params.phone;
			local.company.fax = params.fax;
			
			local.company.save();
			
			flashInsert(info="Successfully add company");
			redirectTo(action="home");
			
		</cfscript>
		
	</cffunction>
	
	<cffunction name="editCompany" access="public" hint="edit company" description="edit company">
		
		<cfscript>
			
			if (not StructKeyExists(params, "id")) {
				flashInsert(error="Failed load edit page");
				redirectTo(action="home");
			}
			
			local.id = Right(params.id, Len(params.id) - 3);
			
			try {
				local.id = decryptHex(local.id);
			}
			catch(Any e) {
				flashInsert(error="Failed load edit page");
				redirectTo(action="home");
			}
			
			if (not IsNumeric(local.id)) {
				flashInsert(error="Failed load edit page");
				redirectTo(action="home");
			}
			
			company = model("company").findAll(where="id = #local.id#");
			
			
		</cfscript>
		
	</cffunction> 
	
	<cffunction name="update" access="public" hint="create" description="create">
		
		<cfscript>
			
			if (not Len(Trim(params.company))) {
				flashInsert(error="Required : Company");
				redirectTo(action="home");
			}
			
			try {
				local.id = decryptHex(params.id)
			}
			catch(Any e) {
				flash(error="Failed to update company");
				redirectTo(action="home");
			}
			
			company = model("company").findOne(where="id = #local.id#");
			
			company.companyName = params.company;
			company.address = params.address;
			company.phone = params.phone;
			company.fax = params.fax;
			
			company.save();
			
			flashInsert(info="Successfully edit company");
			redirectTo(action="home");
			
		</cfscript>
		
	</cffunction>
	
	<cffunction name="deleteCompany" access="public" hint="edit company" description="edit company">
		
		<cfscript>
			
			if (not StructKeyExists(params, "id")) {
				flashInsert(error="Failed to delete Company");
				redirectTo(action="home");
			}
			
			local.id = Right(params.id, Len(params.id) - 3);
			
			try {
				local.id = decryptHex(local.id);
			}
			catch(Any e) {
				flashInsert(error="Failed to delete Company");
				redirectTo(action="home");
			}
			
			if (not IsNumeric(local.id)) {
				flashInsert(error="Failed to delete Company");
				redirectTo(action="home");
			}
			
			company = model("company").deleteAll(where="id = #local.id#");
			
			flashInsert(info="Successfully delete company");
			redirectTo(action="home");
			
		</cfscript>
		
	</cffunction>
	
</cfcomponent>